﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using Agresso.Interface.Layer.Service;
using Agresso.Interface.Layer.Domain;
using System.Diagnostics;

namespace Agresso.Interface.Layer.WinServiceTest
{
    public class TaskSchedule
    {
        private Timer m_timer;
        private static EventLog evt = new EventLog("Application");
        public delegate void Job();
        Job m_Job;
        string m_configuration;
        ConfigurationSettingService m_confService;

        public TaskSchedule(string configuration, double interval, Job job) 
        {
            m_Job = job;
            m_configuration = configuration;
            m_confService = new ConfigurationSettingService();
            m_timer = new Timer(interval);
            m_timer.Elapsed += new ElapsedEventHandler(m_timer_Elapsed);
            m_timer.Start();
        }

        void m_timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            try
            {
                ConfigurationSetting cs = m_confService.GetConfigurationSetting(m_configuration);
                if (Convert.ToBoolean(cs.val_11))
                    ExecuteJob(cs);
            }
            catch (Exception x)
            {
                evt.WriteEntry(string.Format("The Execute {2} event at {0} Error : {1}", e.SignalTime, x.Message, m_configuration));
            }
        }
       
        private void ExecuteJob(ConfigurationSetting cs)
        {
            DateTime startDate = Convert.ToDateTime(cs.val_4);
            int everyD = Convert.ToInt16(cs.val_3);
            bool isEndDate = Convert.ToBoolean(cs.val_5);
            DateTime endDate = Convert.ToDateTime(cs.val_6);
            bool isRepeatTask = Convert.ToBoolean(cs.val_7);
            int everyTask = Convert.ToInt16(cs.val_8);
            int durationTask = Convert.ToInt16(cs.val_9);

            string timenow = DateTime.Now.Hour + ":" + DateTime.Now.Minute;
            string[] executeTime = cs.val_2.Split(':');
            DateTime executeDateTime = DateTime.Today.AddHours(Convert.ToInt16(executeTime[0])).AddMinutes(Convert.ToInt16(executeTime[1]));
            string executeTimeRepeat = "";

            if (DateTime.Today.Date.Subtract(startDate.Date).Days % everyD == 0)
            {
                if (isEndDate)
                {
                    if (DateTime.Today.Date <= endDate.Date)
                    {
                        if (timenow == cs.val_2)
                        {
                            m_Job();
                        }
                        else
                        {
                            if (isRepeatTask)
                            {
                                int times = durationTask / everyTask;
                                for (int i = 1; i <= times; i++)
                                {
                                    executeDateTime = executeDateTime.AddMinutes(everyTask);
                                    executeTimeRepeat = executeDateTime.Hour + ":" + executeDateTime.Minute;
                                    if (executeTimeRepeat == timenow)
                                    {
                                        m_Job();
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (timenow == cs.val_2)
                    {
                        m_Job();
                    }
                    else
                    {
                        if (isRepeatTask)
                        {
                            int times = durationTask / everyTask;
                            for (int i = 1; i <= times; i++)
                            {
                                executeDateTime = executeDateTime.AddMinutes(everyTask);
                                executeTimeRepeat = executeDateTime.Hour + ":" + executeDateTime.Minute;
                                if (executeTimeRepeat == timenow)
                                {
                                    m_Job();
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
